package net.onceface.core.domain.mappedsuper;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;

/**
 * Party 实体类。是类目（category）、店铺（shop）和用户（user）的共同基类。
 * Party是系统核心类型，一个非常关键的抽象。
 * 
 */
@MappedSuperclass
public abstract class Party extends TimeIntervalEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1912229299631595812L;
	
	/**
	 * 名称
	 * **/
	@Column(name="NAME",nullable = false)
	private String name;
	
	/**
	 * 级别
	 * **/
	@Column(name = "LEVEL")
	private Integer level;
	
	/**
	 * 排序
	 * **/
	@Column(name = "SORT_ORDER")
	private Integer sortOrder;

	public Party() {
	}
	
	public Party(String name, int level) {
		this.name = name;
		this.level = level;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getLeve() {
		return level;
	}

	public void setLeve(Integer level) {
		this.level = level;
	}

	public Integer getSortOrder() {
		return sortOrder;
	}

	public void setSortOrder(Integer sortOrder) {
		this.sortOrder = sortOrder;
	}
	
	@Override
	public void validate() {
		super.validate();
	}
	
	/**
	 * 根据标识从仓储中获取实体
	 * @param id
	 * @return
	 */
	public static Party get(Long id) {
		return getRepository().get(Party.class, id);
	}
	
	/**
	 * 根据名称从仓储中获取实体
	 * @param serialNumber
	 * @return
	 */
	public static <T extends Party> T findByName(Class<T> partyClass, String name) {
		return getRepository().createCriteriaQuery(partyClass).eq("Name", name).singleResult();
	}

	@Override
	public String toString() {
		return "Party [name=" + name + ", leve=" + level
				+ ", sortOrder=" + sortOrder + "]";
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (!(obj instanceof Party))
			return false;
		Party other = (Party) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (level != other.level)
			return false;;
		if (sortOrder != other.sortOrder)
			return false;
		return true;
	}
}
